local FightHelper = {}
local hero_res = require("config.hero_res")

function FightHelper.getHeroHitTime(name)
    local config = hero_res.get(name)
    assert(config, "can not found config in hero_res by id=" .. name)
    return config.hit_time
end

function FightHelper.calcHurt(attr, value)
    local AttributeConst = require("const.AttributeConst")
    local defense = attr:getAttrValue(AttributeConst.DEFENSE)
    local hurt = value * 10 - defense
    hurt = hurt < 0 and 0 or hurt
    return hurt
end

function FightHelper.getEffectOffset(config)
    local Vector3 = require("utils.Vector3")
    local xyz = string.split(config.effect_offset, ",")
    return Vector3.new(tonumber(xyz[1]), tonumber(xyz[2]), tonumber(xyz[3]))
end

-- FightConst.SKILL_TARGET_SINGLE = 1 -- 单人
-- FightConst.SKILL_TARGET_ALL = 2 -- 全体
-- FightConst.SKILL_TARGET_COLUMN = 3 -- 一列
-- FightConst.SKILL_TARGET_ROW = 4 -- 一行
-- FightConst.SKILL_TARGET_RANDOM = 5 -- 随机
-- FightConst.SKILL_TARGET_CALL = 6 -- 召唤
function FightHelper._SKILL_TARGET_1(param)
    local enemys = param.enemys
    local entityId = param.entityId
    if param.forEntityId and param.forEntityId > 0 then
        entityId = param.forEntityId
    end
    local result = {}
    if entityId then
        for i = 1, #enemys do
            if enemys[i]:getId() == entityId then
                table.insert(result, enemys[i])
            end
        end
    else
        local index = math.random(1, #enemys)
        table.insert(result, enemys[index])
    end
    return result
end

function FightHelper._SKILL_TARGET_2(param)
    return param.enemys
end

function FightHelper._SKILL_TARGET_3(param)
    local enemys = param.enemys
    local entityId = param.entityId
    if param.forEntityId and param.forEntityId > 0 then
        entityId = param.forEntityId
    end
    local result = {}
    local col = 0
    if entityId then
        for i = 1, #enemys do
            if enemys[i]:getId() == entityId then
                col = enemys[i]:getStandCol()
            end
        end
    else
        local index = math.random(1, #enemys)
        col = enemys[index]:getStandCol()
    end
    for i = 1, #enemys do
        if enemys[i]:getStandCol() == col then
            table.insert(result, enemys[i])
        end
    end
    return result
end

function FightHelper._SKILL_TARGET_4(param)
    local enemys = param.enemys
    local entityId = param.entityId
    if param.forEntityId and param.forEntityId > 0 then
        entityId = param.forEntityId
    end
    local row = 0
    local result = {}
    if entityId then
        for i = 1, #enemys do
            if enemys[i]:getId() == entityId then
                row = enemys[i]:getStandRow()
            end
        end
    else
        local index = math.random(1, #enemys)
        row = enemys[index]:getStandRow()
    end
    for i = 1, #enemys do
        if enemys[i]:getStandRow() == row then
            table.insert(result, enemys[i])
        end
    end
    return result
end

function FightHelper._SKILL_TARGET_5(param)
    local enemys = param.enemys
    local amount = param.amount
    if param.forEntityId and param.forEntityId > 0 then
        local forEntity = nil
        for i = 1, #enemys do
            if enemys[i]:getId() == param.forEntityId then
                forEntity = enemys[i]
                table.remove(enemys, i)
                break
            end
        end
        local result = FightHelper.shuffleGetList(enemys, amount - 1)
        table.insert(result, 1, forEntity)
    else
        return FightHelper.shuffleGetList(enemys, amount)
    end
end

function FightHelper._SKILL_TARGET_6(param)
    return {}
end

function FightHelper.shuffleGetList(list, num)
    local result = {}
    for i = 1, num do
        if #list > 0 then
            local index = math.random(1, #list)
            table.insert(result, list[index])
            table.remove(list, index)
        end
    end
    return result
end

function FightHelper.shuffleGetRowList(list)
    local index = math.random(1, #list)
    local en = list[index]
    local result = {}
    for i = 1, #list do
        if list[i]:getStandRow() == en:getStandRow() then
            table.insert(result, list[i])
        end
    end
    return result
end

function FightHelper.shuffleGetColList(list)
    local index = math.random(1, #list)
    local en = list[index]
    local result = {}
    for i = 1, #list do
        if list[i]:getStandCol() == en:getStandCol() then
            table.insert(result, list[i])
        end
    end
    return result
end

function FightHelper.getNearActor(actor, targetActors)
    local Vector3 = require("utils.Vector3")
    local myPos = actor.transform.position
    local minDis = 0
    local minActor = nil
    for i = 1, #targetActors do
        local tarActor = targetActors[i].actor
        local tarPos = tarActor.transform.position
        local dis = Vector3.distance(myPos, tarPos)
        if dis < minDis or minDis == 0 then
            minDis = dis
            minActor = tarActor
        end
    end
    return minActor
end

function FightHelper.generateHurtFly(actor, hurt)
    local param = {}
    local position = actor.transform.position
    param.position = position
    param.value = hurt.value
    param.isCrit = hurt.crit or false
    param.element = hurt.element
    param.parent = actor
    param.absorb = hurt.absorb
    return param
end

function FightHelper.getActorCenterPos(actors)
    local pos = actors[1].transform.position
    for i = 2, #actors do
        local position = actors[i].transform.position
        pos.x = pos.x + position.x
        pos.z = pos.z + position.z
    end
    pos.x = pos.x / #actors
    pos.z = pos.z / #actors
    return pos
end

function FightHelper.getFightMonsterHeros(monster_team_id)
    local cfg = require("config.monster_team").get(monster_team_id)
    local result = {}
    for i = 1, 6 do
        local param = {}
        if cfg["monster_" .. i] > 0 then
            param.cfg = require("config.monster").get(cfg["monster_" .. i])
            param.level = cfg.level
        end
        table.insert(result, param)
    end
    return result
end

function FightHelper.actorFactory(cfg, obj, manager)
    local actor = nil
    if cfg.show == "FightActor" then
        local FightActor = require("gameLogic.fight.FightActor")
        local config = require("config.hero_res").get(cfg.res)
        assert(config, "can not found cfg in hero_res by id=" .. cfg.res)
        actor = FightActor.new(config, manager, obj.entityId)
        actor:setElement(obj.element)
        actor:setName(cfg.name)
    elseif cfg.show == "SimpleActor" then
        local SimpleActor = require("gameLogic.fight.SimpleActor")
        actor = SimpleActor.new(manager, obj.entityId, cfg.res)
    elseif cfg.show == "StandingActor" then
        local StandingActor = require("gameLogic.fight.StandingActor")
        actor = StandingActor.new(manager, obj.entityId, cfg.res)
    end
    return actor
end

function FightHelper.getMonsterTeamShowRes(monsterteamId)
    local teamCfg = require("config.monster_team").get(monsterteamId)
    local resCfg = require("config.hero_res").get(teamCfg.res_id)
    return resCfg.res_name, teamCfg.name
end

return FightHelper
